<?php
import('@.Model.Extend.UserViewModel');
import('@.Model.Where.AccountWhereMachine');
class UserAction extends Action{
    static $COUNT_PER_PAGE=15;
    public function index(){
       return $this->search(); 
    }
    public function search(){
        $am= new UserViewModel();
        $map= new AccountWhereMachine();
        //将role值转为正确的数值
        $role= AccountModel::C('ROLE_USER_BASIC');
        if (! is_null($role)){
            $map->role($role);
            $this->assign('role', $role);
        }
        if (! is_null($q=$_GET['q'])){
            $map->name($q);
        }
        //分页
        import("@.Common.NoticePage");
        $count=$am->where($map->get())->count();
        $page= new NoticePage($count, self::$COUNT_PER_PAGE);
        $this->assign('page', $page->show());
        
        $v= $am->limit($page->firstRow.','.$page->listRows)->where($map->get())->order('`uid` ASC')->select();
        $this->assign('accounts', $v);
        
        $this->display('index');
    }
    /****
    * @id 指定id，可以为数组
    * @state 新的用户权限
    */    
    public function state(){
        if (! $_REQUEST['submit']){
            return;
        }
        $id= $_REQUEST['id'];
        //ThinkPHP Db.class 将自动处理IN的数组转换
        $map=array('uid'=> array('IN', $id));
        $am= new AccountModel();
        $state= $am->checkState($_REQUEST['state']);    //校验state
        $res= $am->where($map)->setField('state', $state);
        
        if (!$res){
            $this->error(array('message'=>"failed"));
        } else{
            $this->success(array('message'=>$res."你好"));
        }
    }
    /****
    * @id 指定uid，可以为数组
    * @role 新的用户组别
    */
    public function role(){
        if (! $_REQUEST['submit']){
            return;
        }
        $id= $_REQUEST['id'];
        $map=array('uid'=> array('IN', $id));
        $am= new AccountModel();
        $role= $am->checkRole($_REQUEST['role']);    //校验state
        $res= $am->where($map)->setField('role', $role);
        
        if (false===$res){
            $this->error(array('message'=>"failed to change the role"));
        } else{
            $this->success(array('message'=>$res));
        }
    }
    /****
    * 删除
    * @id 指定uid，可以为数组
    */
    public function del(){
        if (! $_REQUEST['submit']){
            return;
        }
        $id= $_REQUEST['id'];
        $map=array('uid'=> array('IN', $id));
        $am= new AccountModel();
        $res= $am->where($map)->delete();
        
        if (!$res){
            $this->error(array('message'=>"failed"));
        } else{
            $this->success(array('message'=>$res));
        }
    }
    public function test(){
        $msg= array('status'=>1, 'message'=>'2hello', 'data'=>false);
        dump(json_encode($msg));
    }

}
?>